<?php
/**
 * Created by PhpStorm.
 * User: John
 * Date: 16-12-29
 * Time: 下午5:04
 */
namespace app\wxbackend\model;

use think\Model;

class Building extends Model
{
    private $buildingTable = 'z_building';
    private $houseTable = 'z_house';
    private $bankAreaTable = 'z_bank_area';
    private $memberTable = 'z_member';

    public function __construct() {
        parent::__construct();
    }

    /**
     * 获取房源数据
     * @date   2017-12-29
     * @author Lanlc
     *
     * @param  int      $mid       用户ID
     * @param  int      $pdtype     父账号类型
     * @param  int      $dtype     员工账号类型
     * @param  int      $buildId   楼宇id
     * @param  int      $province  省
     * @param  int      $city      市
     * @param  string   $country   区
     * @param  string   $building  楼宇名称
     * @param  int      $address   楼宇地址
     * @param  int      $tradeArea 商圈属性
     * @param  int      $status    房屋状态，1已出租，2未出租
     * @param  string   $room      房号
     * @param  int      $isDelete  房源失效状态 1=失效 0=未失效
     * @param  array    $trustHouse      托管的房源id数组(运营方和托管方的)
     * @param  array    $auth       限制的房源id
     * @param  int      $page    偏移量
     * @param  int      $pageSize  数量
     *
     * @return array
     */
    public function getBuildingHouse($mid, $pdtype, $dtype, $buildId, $province, $city, $country, $building, $address, $tradeArea, $status, $room = '', $isDelete, $trustHouse = [], $auth = [], $page, $pageSize)
    {
        $where = [];
        $map   = '';

        if ($dtype == 7) { //员工
            if (!$auth) {
                return [];
            } else {
                $where['h.id'] = ['in', $auth];
            }
        } else { //父账号
            if ($dtype == 1) {
                //业主查自己的
                $map = "h.mid = $mid";
            } elseif (in_array($dtype, [3, 4])) {

                //托管，运营方查自己的房源信息 和已有托管的房源信息
                if (!empty($trustHouse)) {
                    $trustHouse = '('. implode(',', $trustHouse). ')';
                    $map = "(h.mid = $mid) OR (h.id in $trustHouse)";
                } else {
                    $map = "h.mid = $mid";
                }
            } elseif ($dtype == 5) {//物业公司
                $where['b.property_id'] = $mid;
            } elseif ($dtype == 6) {//水电公司
                $where['b.hydropower_id'] = $mid;
            }
            //超管不做限制
        }



        if ($buildId) {
            $where['h.building_id'] = $buildId;
        }

        if ($province) {
            $where['b.province'] = $province;
        }
        if ($city) {
            $where['b.city'] = $city;
        }
        if ($country) {
            $where['b.country'] = $country;
        }
        if ($building) {
            $where['b.name'] = ['like', '%' . $building . '%'];
        }

        if ($address) {
            $where['b.address'] = ['like', '%' . $address . '%'];
        }
        if ($tradeArea) {
            $where['b.trade_area'] = $tradeArea;
        }

        if (in_array($status, [1, 2])) {
            $where['h.status'] = $status;
        }
        if ($room) {
            $where['h.room'] = $room;
        }
        if (in_array($isDelete, [0, 1])) {
            $where['h.is_delete'] = $isDelete;
        }
        //$where['h.is_delete'] = 0;//未删除的
        //$where['b.is_delete'] = 0;
        $field  = 'h.id, b.name as bname, b.type, h.status, b.province, b.city, b.country, b.address,
                  b.building_number, b.building_count, b.trade_area,b.mid,
                  h.room as number,h.area, h.house_number, h.is_delete';
        $res    = $this->setTable($this->houseTable . ' h')->join($this->buildingTable . ' b', 'b.id=h.building_id', 'LEFT')
            ->field($field)->where($where)->where($map)->order('b.id', 'ASC')->page($page, $pageSize)->select();
        if (!$res) {
            return [];
        }

        $sum  = $this->setTable($this->houseTable . ' h')->join($this->buildingTable . ' b', 'b.id=h.building_id', 'LEFT')->where($where)->where($map)->count();
        $data = [
            'list'  => $res,
            'dtype' => $pdtype,
            'page'  => $page,
            'total' => ceil($sum / $pageSize),
        ];
        return $data;
    }

    /**
     * 获取楼宇数据
     * @date   2017-12-29
     * @author Lanlc
     *
     * @param  int      $mid       用户ID
     * @param  int      $dtype     用户类型
     * @param  int      $province  省
     * @param  int      $city      市
     * @param  string   $country   区
     * @param  int      $building  楼宇ID
     * @param  int      $tradeArea 商圈属性
     * @param  int      $isDelete  是否失效 1=是 0=否
     * @param  int      $page      页数
     * @param  int      $pageSize  数量
     *
     * @return
     */
    public function getBuilding($mid, $dtype,  $province, $city, $country, $building, $tradeArea, $isDelete, $page, $pageSize)
    {
        $where = [];


        if (in_array($dtype, [3, 4])) {
            //托管，运营方查自己发布的楼宇, 其他人查所有
            $where['mid'] = $mid;
        } elseif ($dtype == 5) {//物业公司
            $where['property_id'] = $mid;
        } elseif ($dtype == 6) {//水电公司
            $where['hydropower_id'] = $mid;
        }


        if ($province) {
            $where['province'] = $province;
        }

        if ($city) {
            $where['city']     = $city;
        }

        if ($country) {
            $where['country']  = $country;
        }

        if ($building) {
            $where['name'] = ['like', '%'.$building.'%'];
        }

        if ($tradeArea) {
            $where['trade_area'] = $tradeArea;
        }

        if (in_array($isDelete, [0, 1])) {
            $where['is_delete'] = $isDelete;
        }
        //失效的楼宇可以查出来
        //$where['is_delete'] = 0;
        $field = 'id as building_id, name, type, status, province, city, country, address,trade_area,
                  building_number,building_count, mid, is_delete, fee_ratio, lift_num, parking_num, building_floor';
        $res = $this->setTable($this->buildingTable)->field($field)->where($where)->order('id', 'ASC')->page($page, $pageSize)->select();
        if (!$res) {
            return [];
        }

        $sum = $this->setTable($this->buildingTable)->where($where)->count();
        $data = [
            'list'  => $res,
            'dtype' => $dtype,
            'page'  => $page,
            'total' => ceil($sum / $pageSize),
        ];
        return $data;
    }

    /**
     * 获取某楼宇的房源总数
     * @date   2017-3-7
     * @author wuhao
     *
     * @param  int|array  $buildId  楼宇编号
     * @return
     */
    public function getHouseCntByBuildId($buildId) {
        if (!$buildId || (!is_numeric($buildId) && !is_array($buildId))) {
            return false;
        }

        if (is_array($buildId)) {
            $where = [
                'is_delete'   => 0,
                'building_id' => ['in', $buildId],
            ];
        } else {
            $where = [
                'is_delete'   => 0,
                'building_id' => $buildId,
            ];
        }

        return $this->setTable($this->houseTable)->field('count(id) as house_cnt, building_id')->where($where)->group('building_id')->select();
    }

    /**
     * 删除楼宇
     * @date   2017-3-7
     * @author wuhao
     *
     * @param  int  $buildingId  楼宇id
     * @return bool
     */
    public function deleteBuilding($buildingId)
    {
        if (!is_numeric($buildingId) || $buildingId <= 0) {
            return false;
        }
        return $this->setTable($this->buildingTable)->delete($buildingId);
    }

    /**
     * 增加楼宇
     * @date   2017-12-29
     * @author Lanlc
     *
     * @return
     */
    public function addBuilding($data)
    {
        $res =  $this->setTable($this->buildingTable)->insertGetId($data);
        return $res;
    }

    /**
     * 增加房源
     * @date   2017-12-29
     * @author Lanlc
     *
     *
     * @return
     */
    public function addHouse($data)
    {
        $res =  $this->setTable($this->houseTable)->insertGetId($data);
        return $res;
    }

    /**
     * 更新房源
     * @date   2017-12-29
     * @author Lanlc
     *
     *
     * @return
     */
    public function updateBuilding($data, $where)
    {
        $res =  $this->setTable($this->buildingTable)->where($where)->update($data);
        return $res;
    }

    /**
     * 更新房源
     * @date   2017-12-29
     * @author Lanlc
     *
     *
     * @return
     */
    public function updateHouse($data, $where)
    {
        $res =  $this->setTable($this->houseTable)->where($where)->update($data);
        return $res;
    }

    /**
     * 删除房源
     * @date   2017-3-14
     * @author wuhao
     *
     * @param int  $houseId  房源id
     * @return
     */
    public function deleteHouse($houseId)
    {
        if (!is_numeric($houseId) || $houseId<=0) {
            return false;
        }
        $where = ['id' => $houseId];
        return $this->setTable($this->houseTable)->where($where)->delete();
    }

    /**
     * 检测某栋楼下是否有房源
     * @date   2017-4-1
     * @author wuhao
     *
     * @param  int  $buildingId 楼宇id
     * @return
     */
    public function checkHouseByBuildingId($buildingId)
    {
        if (!is_numeric($buildingId) || $buildingId <= 0) {
            return false;
        }
        $where = ['building_id' => $buildingId];
        return $this->setTable($this->houseTable)->where($where)->count();
    }

    /**
     * 禁用某栋楼下的房源
     * @date   2017-3-14
     * @author wuhao
     *
     * @param  int  $buildingId 楼宇id
     * @return
     */
    public function forbidHouseByBuildingId($buildingId)
    {
        if (!is_numeric($buildingId) || $buildingId <= 0) {
            return false;
        }
        $where = ['building_id' => $buildingId];
        $data  = ['is_delete' => 1];
        return $this->setTable($this->houseTable)->where($where)->update($data);
    }

    /**
     * 删除某栋楼下的房源
     * @date   2017-3-14
     * @author wuhao
     *
     * @param  int  $buildingId 楼宇id
     * @return
     */
    public function deleteHouseByBuildingId($buildingId) {
        if (!is_numeric($buildingId) || $buildingId <= 0) {
            return false;
        }
        $where = ['building_id' => $buildingId];
        return $this->setTable($this->houseTable)->where($where)->delete();
    }

    /**
     * 获取单个楼宇信息
     * @date   2017-12-30
     * @author Lanlc
     *
     * @param  int     $buildingId  房源ID
     * @param  string  $field    数据字段
     *
     * @return
     */
    public function getBuildingInfo($buildingId, $field)
    {
        return $this->setTable($this->buildingTable)->field($field)->where(['id' => $buildingId])->find();
    }

    /**
     * 获取单个房源信息
     * @date   2017-12-30
     * @author Lanlc
     *
     * @param  string  $field    数据字段
     * @param  array   $where    查询条件
     *
     * @return
     */
    public function getHouseInfo($field, $where, $page=0, $offSet=1)
    {
        return $this->setTable($this->houseTable. ' h')
                ->join($this->buildingTable. ' b', 'h.building_id=b.id', 'LEFT')
                ->field($field)->where($where)->limit($page, $offSet)->select();
    }

    /**
     * 获取单个房源信息总数
     * @date   2017-01-12
     * @author Lanlc
     *
     * @param  string  $field    数据字段
     * @param  array   $where    查询条件
     *
     * @return
     */
    public function getHouseInfoCnt($where)
    {
        return $this->setTable($this->houseTable. ' h')
            ->join($this->buildingTable. ' b', 'h.building_id=b.id', 'LEFT')
            ->where($where)->count();
    }

    /**
     * 获取用户发布的楼宇信息
     * @date   2017-1-9
     * @author Lanlc
     *
     * @param  int  $mid  用户ID
     *
     * @return
     */
    public function getOwnerBuildings($mid)
    {
        return  $this->setTable($this->buildingTable)
            ->where(['mid' => $mid, 'is_delete' => 0])
            ->field('province, city, country, name, id')
            ->select();
    }


    /**
     * 校验楼宇唯一性
     * @date   2017-1-9
     * @author Lanlc
     *
     * @param  string  $name            楼宇名称
     * @param  string  $buildingNumber  楼宇栋数(F1,F2这样)
     * @param  int     $province        省
     * @param  int     $city            市
     * @param  string  $country         区
     *
     * @return
     */
    public function checkBuilding($name,$buildingNumber, $province, $city, $country)
    {
        $where = [
            'name'            => $name,
            'province'        => $province,
            'city'            => $city,
            'country'         => $country,
            'building_number' => $buildingNumber,
        ];
        $res = $this->setTable($this->buildingTable)->field('id')->where($where)->find();
        return $res ? $res : false;
    }

    /**
     * 校验房源唯一性
     * @date   2017-1-9
     * @author Lanlc
     *
     * @param  array  $where  检验参数
     *
     * @return
     */
    public function checkHouse($where)
    {
        return $this->setTable($this->houseTable)->field('id')->where($where)->find();
    }

    /**
     * 校验房源唯一性
     * @date   2017-1-9
     * @author wuhao
     *
     * @param  int  $buildId  楼宇id
     * @param  int  $number   楼栋
     * @param  int  $floor    层数
     * @param  int  $room     房号
     *
     *
     * @return
     */
    public function checkHouseOwner($buildId, $number, $floor, $room)
    {
        $where = [
            'building_id' => $buildId,
            'number'      => $number,
            'floor'       => $floor,
            'room'        => $room
        ];
        return $this->setTable($this->houseTable)->field('id')->where($where)->find();
    }

    /**
     * 获取省份
     * @date   2017-01-10
     * @author Lanlc
     *
     * @return
     */
    public function getBankProvince()
    {
        return $this->setTable($this->bankAreaTable)->where(['parent_id' => 0])->select();
    }

    /**
     * 获取市
     * @date   2017-01-10
     * @author Lanlc
     *
     * @return
     */
    public function getBankArea($provinceId)
    {
        return $this->setTable($this->bankAreaTable)->where(['parent_id' => $provinceId])->select();
    }

    /**
     * 托管结算 -- 获取房源数据
     * @date   2017-1-20
     * @author wuhao
     *
     * @param  int     $mid      用户id
     * @param  int     $dtype    用户类型
     * @param  int     $province 省份
     * @param  int     $city     城市
     * @param  string  $country  区域
     * @param  string  $name     房源名称
     * @param  array   $auth     权限限制
     * @param  int     $page     页数
     * @param  int     $pageSize 每页个数
     * @return array
     */
    public function getTrustBuilding($mid, $dtype, $province = '', $city = '', $country = '', $name = '', $auth = [], $page = 1, $pageSize = 3)
    {
        if (is_numeric($province) && $province>0) {
            $where['b.province'] = $province;
        }

        if (is_numeric($city) && $city>0) {
            $where['b.city']     = $city;
        }

        if ($country) {
            $where['b.country']  = $country;
        }

        if ($name) {
            $where['b.name'] = ['like', '%' . $name . '%'];
        }

        if ($dtype == 7) {
            if (!$auth) {
                return [];
            } else {
                $where['h.id'] = ['in', $auth];
            }
        }

        $where['h.mid'] = ['neq', $mid]; //自己的房源除外
        $where['h.status']       = 2;//未出租的
        $where['h.trust_status'] = 0;//未托管的
        $where['h.is_delete']    = 0;//未删除的
        $where['b.is_delete']    = 0;
        $field = 'b.id, b.name, b.province, b.city, b.country, b.address, b.building_number, count(h.house_number) building_count, b.trade_area';
        $res = $this->setTable($this->buildingTable . ' b')
            ->field($field)
            ->join($this->houseTable.' h', 'h.building_id=b.id', 'LEFT')
            ->where($where)
            ->group('b.id')
            ->having('count(h.house_number)>0')
            ->page($page, $pageSize)
            ->select();
        if (!$res) {
            return [];
        }

        $sum =  $this->setTable($this->buildingTable . ' b')
            ->field('count(distinct b.id) sum')
            ->join($this->houseTable.' h', 'h.building_id=b.id', 'LEFT')
            ->where($where)
            ->having('count(h.house_number)>0')
            ->find();
        $data   = [
            'list'         => $res,
            'current_page' => $page,
            'total'        => ceil($sum['sum'] / $pageSize),
        ];
        return $data;
    }

    /**
     * 托管结算 -- 根据楼宇ID获取对应的房源信息
     * @date   2017-1-20
     * @author wuhao
     *
     * @param  int  $id       楼宇ID
     * @param  int  $dtype    登陆者类型
     * @param  int  $mid      登陆者id
     * @param  int  $page     页数
     * @param  int  $pageSize 每页个数
     *
     * @return
     */
    public function getHouseByBuildingId($id, $dtype, $mid, $page = 1, $pageSize = 3)
    {
        if (in_array($dtype, [3, 4])) {//托管公司查所有
            $where['building_id']  = $id;
            $where['status']       = 2;//未出租状态
            $where['trust_status'] = 0;//未托管
            $where['mid']          = ['neq', $mid]; //自己的房源除外
        } else {
            return false;
        }

        $where['is_delete'] = 0;//未删除的
        $field = 'house_number, number, floor, room, area, mid';
        $res = $this->setTable($this->houseTable)->field($field)->where($where)->order('mid')->page($page, $pageSize)->select();
        if (!$res) {
            return [];
        }
        $sum = $this->setTable($this->houseTable)->field($field)->where($where)->count();
        $data   = [
            'list'         => $res,
            'current_page' => $page,
            'total'        => ceil($sum / $pageSize),
        ];
        return $data;
    }

    /**
     * 托管结算 -- 根据房源编号获取对应的房源信息
     * @date   2017-1-20
     * @author wuhao
     *
     * @param  int  $id    房源编号
     *
     * @return
     */
    public function getHouseById($id)
    {
        if (!$id) {
            return false;
        }
        $where  = [
            'h.house_number' => $id,
            'h.status'       => 2,//未出租
            'h.is_delete'    => 0, //未删除
        ];
        $field = 'h.house_number, h.mid, m.nickname, m.mobile, b.province, b.city, b.country, b.name, b.address, h.number, h.floor, h.room, h.area';
        $join = [
            [$this->buildingTable. ' b', 'h.building_id=b.id', 'LEFT'],
            [$this->memberTable. ' m','h.mid=m.id', 'LEFT'],
        ];
        $table = $this->houseTable.' h';
        $res = $this->setTable($table)->join($join)->where($where)->field($field)->find();
        return $res;
    }

    /**
     * 获取某业主的房源信息
     * @date   2017-01-23
     * @author Lanlc
     *
     * @param  int     $buildingId  楼宇ID
     * @param  int     $ownerId     业主id
     * @param  int     $trustStatus 托管状态 0=未托管 1=托管中
     * @param  int     $status      出租状态 1=已出租，2=未出租
     * @param  string  $field       字段
     *
     * @return
     */
    public function getRentHouseCnt($buildingId, $ownerId, $trustStatus = -1, $status=0, $field='count(id) as cnt')
    {
        $where['building_id'] = $buildingId;
        if (in_array($trustStatus, [0, 1])) {
            $where['trust_status'] = $trustStatus;
        }
        if ($ownerId) {
            $where['mid'] = $ownerId;
        }
        if ($status && in_array($status, [1, 2])) {
            $where['status']  = $status;
        }
        $where['is_delete'] = 0;//未删除的
        return $this->setTable($this->houseTable)->field($field)->where($where)->find();
    }

    /**
     * 获取托管中的房源的门牌号信息
     * @date   2017-01-23
     * @author wuhao
     *
     * @param  int  $buildingId  楼宇ID
     * @param  int  $ownerId     业主id
     *
     * @return
     */
    public function getHouseRoom($buildingId, $ownerId)
    {
        $where = [
            'building_id' => $buildingId,
            'mid'         => $ownerId,
            'is_delete'   => 0,//未删除的
        ];
        $field = 'number, floor, room';
        return $this->setTable($this->houseTable)->field($field)->where($where)->select();
    }

    /**
     * 获取楼宇名称
     * @date   2017-01-24
     * @author wuhao
     *
     * @param  int  $id  楼宇ID
     *
     * @return
     */
    public function getBuildingName($id)
    {
        $where['id'] = $id;
        $field = 'name';
        return $this->setTable($this->buildingTable)->field($field)->where($where)->find();
    }

    /**
     * 获取对应业主的房源信息
     * @date   2017-02-09
     * @author Lanlc
     *
     * @param  int  $ownerId    业主id
     * @param  int  $buildingId 楼宇id
     *
     * @return
     */
    public function getHouseInfoByUser($ownerId, $buildingId, $field = '*')
    {
        return $this->setTable($this->houseTable)->field($field)->where(['mid' => $ownerId, 'building_id' => $buildingId, 'is_delete' => 0])->select();
    }

    /**
     * 获取某栋楼宇已出租或已托管的房间
     * @date   2017-03-08
     * @author wuhao
     *
     * @param  int  $buildingId  楼宇id
     *
     * @return
     */
    public function getHouseStatusByBuildId($buildingId)
    {
        if (!$buildingId) {
            return false;
        }

        $where = "building_id=:id AND is_delete=:del AND (status=:status OR trust_status=:t_status)";
        $bind = [
            'id'       => [$buildingId, \PDO::PARAM_INT],
            'del'      => [0, \PDO::PARAM_INT],
            'status'   => [1, \PDO::PARAM_INT],
            't_status' => [1, \PDO::PARAM_INT]
        ];
        return $this->setTable($this->houseTable)
            ->field('house_number, status')
            ->where($where)
            ->bind($bind)
            ->select();
    }

    /**
     * 获取楼宇出租金钱总额
     * @date   2017-01-22
     * @author Lanlc
     *
     * @param  int  $buildingId  楼宇ID
     *
     * @return
     */
    public function getBuildingSumMoney($buildingId)
    {
        return $this->setTable($this->houseTable)->field('sum(price*area) as sum')->where(['building_id' => $buildingId, 'is_delete' => 0])
            ->group('building_id')->find();
    }

    /**
     * 获取某个业主的房源信息
     * @date   2017-03-09
     * @author wuhao
     *
     * @param  int    $mid    业主id
     * @param  string $field  字段
     *
     * @return array
     */
    public function getHouseInfoByMid($mid, $field = '*')
    {
        if (!is_numeric($mid)) {
            return false;
        }
        $where = [
            'mid'       => $mid,
            'is_delete' => 0
        ];
        return $this->setTable($this->houseTable)->field($field)->where($where)->select();
    }

    /**
     * 获取某个运营方发布的楼宇信息
     * @date   2017-03-09
     * @author wuhao
     *
     * @param  int    $mid    运营方id
     * @param  string $field  字段
     *
     * @return array
     */
    public function getBuildByMid($mid, $field = '*')
    {
        if (!is_numeric($mid)) {
            return false;
        }
        $where = ['mid' => $mid, 'is_delete' => 0];
        return $this->setTable($this->buildingTable)->field($field)->where($where)->select();
    }

    /**
     * 获取楼宇对应的房源信息
     * @date   2017-03-09
     * @author wuhao
     *
     * @param  int|array $buildingId  楼宇id或楼宇id数组
     * @param  string    $field       字段
     *
     * @return array
     */
    public function getHouseInfoByBuildingId($buildingId, $field = '*')
    {
        if (!$buildingId || (!is_numeric($buildingId) && !is_array($buildingId))) {
            return false;
        }

        if (is_array($buildingId)) {
            $where['building_id'] = ['in', $buildingId];
        } else {
            $where['building_id'] = $buildingId;
        }

        $where['is_delete'] = 0;
        return $this->setTable($this->houseTable)->field($field)->where($where)->select();
    }

    /**
     * 获取楼宇的详细信息
     * @date   2017-03-15
     * @author wuhao
     *
     * @param  int    $buildingId  楼宇id
     * @param  string $field       获取的字段
     *
     * @return array
     */
    public function getBuildingDetail($buildingId, $field = '*')
    {
        if (!is_numeric($buildingId) || $buildingId<=0) {
            return false;
        }
        $where = ['id' => $buildingId];
        return $this->setTable($this->buildingTable)->field($field)->where($where)->find();
    }

    /**
     * 获取房源的详细信息
     * @date   2017-03-16
     * @author wuhao
     *
     * @param  int    $houseId      房源id
     * @param  string $field        获取的字段
     *
     * @return array
     */
    public function getHouseDetail($houseId, $field='*')
    {
        if (!is_numeric($houseId) || $houseId<=0) {
            return false;
        }
        $where = ['id' => $houseId, 'is_delete' => 0];
        return $this->setTable($this->houseTable)->field($field)->where($where)->find();
    }

    /**
     * 房源管理-获取房源统计信息
     * @date   2017-03-16
     * @author wuhao
     *
     * @param  int      $mid       用户ID
     * @param  int      $dtype     用户类型
     * @param  int      $buildId   楼宇id
     * @param  int      $province  省
     * @param  int      $city      市
     * @param  string   $country   区
     * @param  string   $building  楼宇名称
     * @param  int      $address   楼宇地址
     * @param  int      $tradeArea 商圈属性
     * @param  int      $status    房屋状态，1已出租，2未出租
     * @param  array    $trustHouse      托管的房源id数组(运营方和托管方的)
     *
     * @return array
     */
    public function getHouseSummary($mid, $dtype, $buildId, $province, $city, $country, $building, $address, $tradeArea, $status, array $trustHouse = [])
    {
        $where = [];
        $map   = '';
        if ($mid) {
            if ($dtype == 1) {
                //业主查自己的
                $map = "h.mid = $mid";
            } elseif (in_array($dtype, [3, 4])) {
                //托管，运营方查自己的房源信息 和已有托管的房源信息
                if (!empty($trustHouse)) {
                    $trustHouse = '('. implode(',', $trustHouse). ')';
                    $map = "(h.mid = $mid) OR (h.id in $trustHouse)";
                } else {
                    $map = "h.mid = $mid";
                }
            } else {
                return [];
            }
        }

        if ($buildId) {
            $where['h.building_id'] = $buildId;
        }
        if ($province) {
            $where['b.province'] = $province;
        }
        if ($city) {
            $where['b.city'] = $city;
        }
        if ($country) {
            $where['b.country'] = $country;
        }
        if ($building) {
            $where['b.name'] = ['like', '%' . $building . '%'];
        }

        if ($address) {
            $where['b.address'] = ['like', '%' . $address . '%'];
        }
        if ($tradeArea) {
            $where['trade_area'] = $tradeArea;
        }

        if (in_array($status, [1, 2])) {
            $where['h.status'] = $status;
        }
        //$where['h.is_delete'] = 0;//未删除的
        //$where['b.is_delete'] = 0;
        $field  = 'h.house_number, h.area, h.status, h.price, h.price_type';
        $res    = $this->setTable($this->houseTable . ' h')
            ->field($field)
            ->join($this->buildingTable . ' b', 'b.id=h.building_id', 'LEFT')
            ->where($where)
            ->where($map)
            ->select();
        return $res ? $res : false;
    }

    /**
     * 获取房源信息列表
     * @date   2017-03-18
     * @author wuhao
     *
     * @param  array     $where     条件
     * @param  string    $field     字段
     *
     * @return array
     */
    public function getHouseList($where, $field = '*')
    {
        return $this->setTable($this->houseTable)->field($field)->where($where)->select();
    }

    /**
     * 根据楼宇id,楼栋，房号获取房源信息
     * @date   2017-06-05
     * @author wuhao
     *
     * @param  int     $buildingId     楼宇id
     * @param  int     $number         楼栋
     * @param  string  $room           房号
     * @param  string  $field          获取的字段
     *
     * @return array
     */
    public function getHouseInf($buildingId, $number, $room, $field = '*')
    {
        if (!$buildingId || !$number || !$room) {
            return false;
        }
        $where = [
            'building_id' => $buildingId,
            'number'      => $number,
            'room'        => $room,
            'is_delete'   => 0,
        ];
        return $this->setTable($this->houseTable)->field($field)->where($where)->find();
    }

    /**
     * 获取楼宇信息列表
     * @date   2017-03-18
     * @author wuhao
     *
     * @param  array     $where     条件
     * @param  string    $field     字段
     *
     * @return array
     */
    public function getBuildingList($where, $field = '*')
    {
        return $this->setTable($this->buildingTable)->field($field)->where($where)->select();
    }

    /**
     * 根据楼宇编号获取相关收费对象id
     * @date   2016-02-9
     * @author banjin
     * @param  $buildingId     int  楼宇编号
     *
     * @return array
     */
    public function getContractFeeB($buildingId)
    {
        if (!$buildingId) {
            return false;
        }
        $where= [
            'id' => $buildingId
        ];
        $res = $this->table($this->buildingTable)->where($where)->field('mid,property_id,hydropower_id')->find();
        return $res;
    }

    public function getTrustHouseId($trustId)
    {
        $where['trust_id'] = $trustId;
        $where['is_delete'] = 0;
        $res = $this->table($this->houseTable)->where($where)->field('id')->select();
        return $res;
    }

    /**
     * @comment 获取自己所管理的房源
     * @author wuhao
     * @param $memberId  用户id
     * @param $dtype  用户类型
     * @return array
     */
    public function getOwnerHouse($memberId, $dtype)
    {
        $res = [];
        if (!in_array($dtype, [0,1,3,4,5,6,7]) || !$memberId) {
            return $res;
        }
        switch ($dtype) {
            case 0:
                $where = [];
                break;
            case 1:
                $where['mid'] = $memberId;
                break;
            case 3:
            case 4:
                $where['mid|trust_id'] = $memberId;
                break;
            case 5:
            case 6:
                //先查询楼宇
                $string = $dtype == 5 ? 'property_id' : 'hydropower_id';
                $buildingId = $this->getBuildingList([$string => $memberId, 'is_delete' => 0], 'id');
                if (!$buildingId) {
                    return $res;
                } else {
                    $buildIdArr = array_column($buildingId, 'id');
                    $where['building_id'] = ['in', $buildIdArr];
                }
                break;
            case 7:
                $memberModel = new Member();
                $house = $memberModel->getMemberInfoById($memberId, 'house_auth');
                $house = json_decode($house['house_auth'], true);
                if (!$house) {
                    return [];
                } else {
                    $where['id'] = ['in', $house];
                }
                break;
            default :
                return $res;
        }
        $where['is_delete'] = 0;
        $res = $this->table($this->houseTable)->field('id, building_id, house_number, floor, number, room')->where($where)->select();
        return $res ?: [];
    }
}
